package com.tongji.showroom.view
{
	import com.tongji.showroom.events.AddUserProductEvent;
	import com.tongji.showroom.events.DeleteObjEvent;
	import com.tongji.showroom.events.DisplayPictureEvent;
	import com.tongji.showroom.model.PictureModel;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.view.FirstStepUploadRightComp;
	import com.tongji.showroom.vo.Picture;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 这个类是用于显示已上传图片及所选中图片
	 * @author chenzhongyuan
	 * 2011-7-22
	 */
	
	public class FirstStepUploadRightCompMediator extends Mediator
	{
		[Inject]
		public var view:FirstStepUploadRightComp;
		
		[Inject]
		public var model:PictureModel;
		
		[Inject]
		public var userModel:UserModel;
		
		public var picture:Picture;
		
		public var flagUp:int = 3;
		
		public var flagBelow:int = 0;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.briefMessageList.dataGroup, "dataProvider", model, "tempArray");
			//BindingUtils.bindProperty(view.bigPicture, "source", model, "defaultSource");
			view.bigPicture.source = new Bitmap(model.prePic);
			//view.addEventListener(Event.ENTER_FRAME, checkNullHandler);
			addViewListener(DisplayPictureEvent.DISPLAY_A_PICTURE, displayPicture);
			addViewListener(DeleteObjEvent.FIRSTLEFT_DELETE_PIC, deletePicture);
			eventMap.mapListener(view.LeftBtn, MouseEvent.CLICK, leftMove);
			eventMap.mapListener(view.RightBtn, MouseEvent.CLICK, rightMove);
		}
		
		private function checkNullHandler(event:Event):void
		{
			if (model.tempArray.length == 0)
			{
				model.defaultSource = "images/Default.png";
				view.briefMessageList.selectedIndex = -1;
			}			
		}
		
		private function displayPicture(event:DisplayPictureEvent):void
		{
			view.bigPicture.source = event.obj.pImgUrl;	
			event.obj.pictureSelected = true;
			model.defaultSource = event.obj.pImgUrl;				
		}
		
		private function deletePicture(event:DeleteObjEvent):void
		{
			//派发事件删除服务器上的图片
			dispatch(new AddUserProductEvent(AddUserProductEvent.DELETE_USER_PRODUCT, userModel.userID, null, event.obj.pid));
			var location:Number = model.tempArray.getItemIndex(event.obj);
			
			if (model.tempArray[location].pictureSelected == true)
			{
				model.tempArray[location].pictureSelected == false;
				view.bigPicture.source = "images/Default.png";
			}	
			model.tempArray.removeItemAt(location);			
		}
		
		private function leftMove(event:MouseEvent):void
		{
			if (flagUp <=  model.tempArray.length - 2)
			{
				flagUp ++;
				flagBelow ++;
				view.briefMessageList.ensureIndexIsVisible(flagUp);
			}			
		}
		
		private function rightMove(event:MouseEvent):void
		{
			if (flagBelow >=  1)
			{
				flagBelow --;
				flagUp --;
				view.briefMessageList.ensureIndexIsVisible(flagBelow);
			}
		}
	}
}